<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   充值
 */
defined('IN_YuLin') || exit('NO PERMIT!');

if(!$_uid)
{
	header('Location: '.MU('login'));
	exit;
}

$table = Table('user_money_recharge');

if(IS_POST)
{
	$money = intval($_POST['money']);
	($money < 100) && ShowMsg('充值金额不能低于100',-1);
	
	$payment = NoBadStr($_POST['payment']);
	!in_array($payment,['alipay','weixin','unionpay']) && $payment = 'alipay';
	
	$ins = [];
	$ins['uid']		=	$_uid;
	$ins['payment']	=	$payment;
	$ins['money']	=	$money;
	$ins['award']	=	0;	//	奖励
	$ins['status']	=	0;
	$ins['ip']		=	$_Y['ip'];
	$ins['dateline']=	TIMESTAMP;
	$id = $db->add('user_money_recharge',$ins);
	!$id && ShowMsg('充值失败，请重新充值',-1);
	ShowMsg('充值接口正在调试中',-1);
	$ins = [];
	$ins['out_trade_no']	=	'CP'.date('YmdHis').rand(1000,9999).$id;
	$ins['body']			=	'在线充值';
	$ins['attach']			=	$id;
	$ins['total_fee']		=	$money * 100;	//	单位分
	$ins['mch_create_ip']	=	$_Y['ip'];
	
	include_once(LIB_PATH.'payment/swiftpass/'.$payment.'.php');
	
	if($payment != 'alipay')
	{
		$ins['time_start']	=	TIMESTAMP;
		$ins['time_expire']	=	TIMESTAMP + 86400;
	}
	
	$request = new PayRequest();
	$arr = $request->index($ins,'order');
	
	$error = 1;
	$res = [];
	
	if($arr['code_img_url'])
	{
		$error = 0;
		$res['img'] = $arr['code_img_url'];
	}
	
}

if($a == 'log')
{
	$payments = ['alipay'=>'支付宝','weixin'=>'微信支付','unionpay'=>'银联支付'];
	
	$data  = [];
	$where = ' FROM '.$table.' t WHERE t.uid = '.$_uid.' AND t.status > 0';

	$payment = NoBadStr($_GET['payment']);
	$payment && $where .= ' AND t.payment = \''.$payment.'\'';

	$page = max(1,intval($_GET['page']));
	$size = 20;

	$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
	if($tmp['p'])
	{
		$limit = ' LIMIT '.($page - 1) * $size.','.$size;

		$url = MU($c.'/'.$a,['payment'=>$payment]);
		$orderby = ' ORDER BY t.id DESC ';
		
		$data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
		
		//$multi = Multi($url,$page,$tmp['p'],$size);
	}
	
	$tpl->display('wap/recharge_log');
	exit;
}

$tpl->display('wap/recharge');